#include "dialog_cali.h"
#include "stdlib.h"
#include "stdio.h"
#include "bmp_backgrounds.h"

CaliDialog::CaliDialog(HWND parent): PopupDialog(parent)
{
    name ="Calibrate Dialog";

    pLable = new Lable(this, "Center, then press CALIB");
    pButtonOK =new Button(this, "OK");
    pButtonCalibrate =new Button(this, "CALIB");

    pBarCh1 =new Bar(this);
    pBarCh2 =new Bar(this);
    pBarCh3 =new Bar(this);
    pBarCh4 =new Bar(this);
    pBarCh5 =new Bar(this);
    pBarCh6 =new Bar(this);
    pBarCh7 =new Bar(this);
    pBarCh8 =new Bar(this);
    pBarCh9 =new Bar(this);
    pBarCh10 =new Bar(this);
    pBarCh11 =new Bar(this);
    pBarCh12 =new Bar(this);

    //set widget name
    pLable->name ="LableWaring";
    pButtonCalibrate->name ="ButtonCali";
    pButtonOK->name ="ButtonOK";
    pBarCh1->name ="BarCh1";
    pBarCh2->name ="BarCh2";
    pBarCh3->name ="BarCh3";
    pBarCh4->name ="BarCh4";
    pBarCh5->name ="BarCh5";
    pBarCh6->name ="BarCh6";
    pBarCh7->name ="BarCh7";
    pBarCh8->name ="BarCh8";
    pBarCh9->name ="BarCh9";
    pBarCh10->name ="BarCh10";
    pBarCh11->name ="BarCh11";
    pBarCh12->name ="BarCh12";
    

    setSize(160, 60);
    
    pLable->setPosition(20,1);
    
    pButtonCalibrate->setPosition(10,10);
    pButtonOK->setPosition(60,10);

    //set bar positions
    pBarCh1->setPosition(6,21); pBarCh1->setSize(50, 8);
    pBarCh2->setPosition(66,21); pBarCh2->setSize(50, 8);
    pBarCh3->setPosition(121,21); pBarCh3->setSize(50, 8);
    pBarCh4->setPosition(6,31); pBarCh4->setSize(50, 8);
    pBarCh5->setPosition(66,31); pBarCh5->setSize(50, 8);
    pBarCh6->setPosition(121,31); pBarCh6->setSize(50, 8);
    pBarCh7->setPosition(6,41); pBarCh7->setSize(50, 8);
    pBarCh8->setPosition(66,41); pBarCh8->setSize(50, 8);
    pBarCh9->setPosition(121,41); pBarCh9->setSize(50, 8);
    pBarCh10->setPosition(6,51); pBarCh10->setSize(50, 8);
    //pBarCh11->setPosition(66,51); pBarCh11->setSize(50, 8);
    pBarCh11->setPosition(26,51); pBarCh11->setSize(50, 8);
    pBarCh12->setPosition(121,51); pBarCh12->setSize(50, 8);

    pBarCh1->setMaxValue(100); pBarCh1->setMinValue(0); pBarCh1->setValue(50);
    pBarCh2->setMaxValue(100); pBarCh2->setMinValue(0); pBarCh2->setValue(50);
    pBarCh3->setMaxValue(100); pBarCh3->setMinValue(0); pBarCh3->setValue(50);
    pBarCh4->setMaxValue(100); pBarCh4->setMinValue(0); pBarCh4->setValue(50);
    pBarCh5->setMaxValue(100); pBarCh5->setMinValue(0); pBarCh5->setValue(50);
    pBarCh6->setMaxValue(100); pBarCh6->setMinValue(0); pBarCh6->setValue(50);
    pBarCh7->setMaxValue(100); pBarCh7->setMinValue(0); pBarCh7->setValue(50);
    pBarCh8->setMaxValue(100); pBarCh8->setMinValue(0); pBarCh8->setValue(50);
    pBarCh9->setMaxValue(100); pBarCh9->setMinValue(0); pBarCh9->setValue(50);
    pBarCh10->setMaxValue(100); pBarCh10->setMinValue(0); pBarCh10->setValue(50);
    pBarCh11->setMaxValue(100); pBarCh11->setMinValue(0); pBarCh11->setValue(50);
    pBarCh12->setMaxValue(100); pBarCh12->setMinValue(0); pBarCh12->setValue(50);

    connect(&pButtonCalibrate->signal[SIGNAL_BUTTON_UP], this);
    connect(&pButtonOK->signal[SIGNAL_BUTTON_UP], this);
}


CaliDialog::~CaliDialog()
{
    delete pLable;
    delete pButtonOK;
    delete pButtonCalibrate;

    delete pBarCh1;
    delete pBarCh2;
    delete pBarCh3;
    delete pBarCh4;
    delete pBarCh5;
    delete pBarCh6;
    delete pBarCh7;
    delete pBarCh8;
    delete pBarCh9;
    delete pBarCh10;
    delete pBarCh11;
    delete pBarCh12;
    
}

void CaliDialog::draw(Rect *r)
{

   short x,y;
    for( y=rect.y0; y<rect.y1; y+=bitmapBG4.h)
    {
        for(x=rect.x0; x<rect.x1; x+=bitmapBG4.w)
        {
            pDC->drawBmp(x, y, x+bitmapBG4.w, y+bitmapBG4.h, bitmapBG4.data);
         }
    }

    pDC->drawRect(rect.x0, rect.y0, rect.x1, rect.y1, 0x0F);

    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));

    Widget::draw(r);
}


void CaliDialog::calibrate()
{
#ifdef DEBUG)FRGUI
    printf("calibrate button down\n");
#endif
    extern Dialog *pMainDialog;
    sendMessage(pMainDialog, MSG_USER_CALIBRATE_DOWN, 0,0);
}

int CaliDialog::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{
    switch(iMsg)
    {
    case MSG_SIGNAL:
        if(SIGNAL_MATCH(wParam, &pButtonCalibrate->signal[SIGNAL_BUTTON_UP]))
        {
            calibrate();
        }
        if(SIGNAL_MATCH(wParam, &pButtonOK->signal[SIGNAL_BUTTON_UP]) )
        {
            close();
        }
        break;
    }
    
    return  PopupDialog::eventHandler(hWnd, iMsg, wParam, lParam);
}


